package com.coolshark.shop.controller;

import com.coolshark.shop.entity.Banner;
import com.coolshark.shop.mapper.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/banner")
public class BannerController {
    @Value("${dirPath}") //从application.properties配置文件中获取配置详细
    private String dirPath;


    @Autowired
    BannerMapper mapper;

    @RequestMapping("/select")
    public List<Banner> select(){
        return mapper.select();
    }

    @RequestMapping("/delete")
    public void delete(int id) {
        System.out.println("id = " + id);
        //得到图片的url
        String url = mapper.selectUrlById(id);
        //删除文件
        new File(dirPath + url).delete();
        mapper.deleteById(id);
    }

    @RequestMapping("/insert")
    public void insert(String url) {
        System.out.println("url = " + url);
        mapper.insert(url);
    }
}
